package com.medicine.controller;

import com.medicine.common.Result;
import com.medicine.exception.BusinessException;
import com.medicine.model.CombatScores;
import com.medicine.service.ICombatScoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 输赢分数记录
 * Created by Guyver.com on 2017/10/27 14:37.
 */
@Controller
@RequestMapping("/api/game/combatscore")
public class CombatScoreController {
    @Autowired
    private ICombatScoreService combatScoreService;
    private Logger logger = LoggerFactory.getLogger(CombatScoreController.class);

    @ResponseBody
    @RequestMapping(value="/add", method= RequestMethod.GET)
    public Result addPlayerCombatScore(@RequestParam String id) {
        try {
            CombatScores combatScores = this.combatScoreService.getPlayerCombatScores(id);
            if(combatScores != null) {
                return new Result(null, "该用户ID已初始化，并分配过了记分记录！，请检查！", 1);
            }
            this.combatScoreService.addPlayerCombatScores(id);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "新增成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/update", method= RequestMethod.POST)
    public Result updatePlayerCombatScore(@RequestBody Object param) {
        try {
            String id = (String)((Map)param).get("id");
            String code = (String)((Map)param).get("code");
            this.combatScoreService.updatePlayerCombatScores(id, code);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "更新成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/query", method= RequestMethod.GET)
    public Result getPlayerCombatScore(@RequestParam String id) {
        try {
            CombatScores combatScores = this.combatScoreService.getPlayerCombatScores(id);
            return new Result(combatScores, "查询成功！", 0);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
    }
}
